********************************************************************************
* Figure A3. YEAR BY YEAR ESTIMATES
********************************************************************************
clear
use "$data/fdiff.dta", clear
set more off

* CONTROLS
global yyctrl feff sh_pgds sh_wap13 sh_college13 sh_manuf13 

* YEARLY ESTIMATES
rename totnet_all_res_d		totnet_all
rename totnet_excl_res_d 	totnet_excl
rename totrev_all_res_d 	totrev_all
rename totrev_excl_res_d 	totrev_excl
rename totexp_all_res_d 	totexp_all
rename totexp_excl_res_d 	totexp_excl

* NET FISCAL COST

	// All budget
matrix A1 = J(5,3,.)
local i=1
levelsof year if year!=2013, local(levels) 
foreach l of local levels {
qui ivreg2 totnet_all ($x = $z) $yyctrl [aw=$w] if year==`l', robust small
matrix A1[`i',1] = _b[$x]

qui twostepweakiv 2sls totnet_all ($x = $z) $yyctrl [aw=$w] if year==`l', robust small
estadd local arci = e(ar_cset)	// Anderson-Rubin CI
di e(arci)
gen ci`l' = e(arci) 	// saves it as a varible in data
split ci`l', parse(,)	// split at ,
replace ci`l'1 = subinstr(ci`l'1, "[", "", .) // removes [
replace ci`l'2 = subinstr(ci`l'2, "]", "", .) // removes ]
destring ci`l'1 ci`l'2, replace
sum ci`l'1
return list
matrix A1[`i',2] = r(mean)	// Low-CI
sum ci`l'2
return list
matrix A1[`i',3] = r(mean)	// High-CI
drop ci`l' ci`l'1 ci`l'2

local ++i 
}

	// Restricted budget
matrix A2 = J(5,3,.)
local i=1
levelsof year if year!=2013, local(levels) 
foreach l of local levels {
qui ivreg2 totnet_excl ($x = $z) $yyctrl [aw=$w] if year==`l', robust small
matrix A2[`i',1] = _b[$x]

qui twostepweakiv 2sls totnet_excl ($x = $z) $yyctrl [aw=$w] if year==`l', robust small
estadd local arci = e(ar_cset)	// Anderson-Rubin CI
di e(arci)
gen ci`l' = e(arci) 	// saves it as a varible in data
split ci`l', parse(,)	// split at ,
replace ci`l'1 = subinstr(ci`l'1, "[", "", .) // removes [
replace ci`l'2 = subinstr(ci`l'2, "]", "", .) // removes ]
destring ci`l'1 ci`l'2, replace
sum ci`l'1
return list
matrix A2[`i',2] = r(mean)	// Low-CI
sum ci`l'2
return list
matrix A2[`i',3] = r(mean)	// High-CI
drop ci`l' ci`l'1 ci`l'2

local ++i 
}

	// Plot
matrix A1=A1'
	matrix A1[1,2] = 0	// Set to zero in 2015. Low first stage F-stat.
	matrix A1[2,2] = 0
	matrix A1[3,2] = 0
matrix A2=A2'
	matrix A2[1,2] = 0	// Set to zero in 2015. Low first stage F-stat.
	matrix A2[2,2] = 0
	matrix A2[3,2] = 0

coefplot (matrix(A1[1]), ci((A1[2] A1[3])) label(All budget) ///
yline(0, lc(gs4)) ciopts(recast(rcap))) ///
(matrix(A2[1]), ci((A2[2] A2[3])) m(D) msize(medsmall) label(Restricted budget) ///
ciopts(recast(rcap)) graphregion(color(white)) ///
xtitle("Estimated effect", size(small)) xlab(, labsize(small))), vertical ///
coeflabel(1 r1 = "2014" ///
		  2 r2 = "2015" ///
		  3 r3 = "2016" ///
		  4 r4 = "2017" ///
		  5 r5 = "2018" , labsize(small))

* REVENUES
set more off
	// All budget
matrix B1 = J(5,3,.)
local i=1
levelsof year if year!=2013, local(levels) 
foreach l of local levels {
qui ivreg2 totrev_all ($x = $z) $yyctrl [aw=$w] if year==`l', robust small
matrix B1[`i',1] = _b[$x]

qui twostepweakiv 2sls totrev_all ($x = $z) $yyctrl [aw=$w] if year==`l', robust small
estadd local arci = e(ar_cset)	// Anderson-Rubin CI
di e(arci)
gen ci`l' = e(arci) 	// saves it as a varible in data
split ci`l', parse(,)	// split at ,
replace ci`l'1 = subinstr(ci`l'1, "[", "", .) // removes [
replace ci`l'2 = subinstr(ci`l'2, "]", "", .) // removes ]
destring ci`l'1 ci`l'2, replace
sum ci`l'1
return list
matrix B1[`i',2] = r(mean)	// Low-CI
sum ci`l'2
return list
matrix B1[`i',3] = r(mean)	// High-CI
drop ci`l' ci`l'1 ci`l'2

local ++i 
}

	// Restricted budget
matrix B2 = J(5,3,.)
local i=1
levelsof year if year!=2013, local(levels) 
foreach l of local levels {
qui ivreg2 totrev_excl ($x = $z) $yyctrl [aw=$w] if year==`l', robust small
matrix B2[`i',1] = _b[$x]

qui twostepweakiv 2sls totrev_excl ($x = $z) $yyctrl [aw=$w] if year==`l', robust small
estadd local arci = e(ar_cset)	// Anderson-Rubin CI
di e(arci)
gen ci`l' = e(arci) 	// saves it as a varible in data
split ci`l', parse(,)	// split at ,
replace ci`l'1 = subinstr(ci`l'1, "[", "", .) // removes [
replace ci`l'2 = subinstr(ci`l'2, "]", "", .) // removes ]
destring ci`l'1 ci`l'2, replace
sum ci`l'1
return list
matrix B2[`i',2] = r(mean)	// Low-CI
sum ci`l'2
return list
matrix B2[`i',3] = r(mean)	// High-CI
drop ci`l' ci`l'1 ci`l'2

local ++i 
}

	// Plot
matrix B1=B1'
	matrix B1[1,2] = 0	// Set to zero in 2015. Low first stage F-stat.
	matrix B1[2,2] = 0
	matrix B1[3,2] = 0
matrix B2=B2'
	matrix B2[1,2] = 0	// Set to zero in 2015. Low first stage F-stat.
	matrix B2[2,2] = 0
	matrix B2[3,2] = 0

coefplot (matrix(B1[1]), ci((B1[2] B1[3])) label(All budget) ///
yline(0, lc(gs4)) ciopts(recast(rcap))) ///
(matrix(B2[1]), ci((B2[2] B2[3])) m(D) msize(medsmall) label(Restricted budget) ///
ciopts(recast(rcap)) graphregion(color(white)) ///
xtitle("Estimated effect", size(small)) xlab(, labsize(small))), vertical ///
coeflabel(1 r1 = "2014" ///
		  2 r2 = "2015" ///
		  3 r3 = "2016" ///
		  4 r4 = "2017" ///
		  5 r5 = "2018" , labsize(small))

* EXPENDITURES
set more off
	// All budget
matrix C1 = J(5,3,.)
local i=1
levelsof year if year!=2013, local(levels) 
foreach l of local levels {
qui ivreg2 totexp_all ($x = $z) $yyctrl [aw=$w] if year==`l', robust small
matrix C1[`i',1] = _b[$x]

qui twostepweakiv 2sls totexp_all ($x = $z) $yyctrl [aw=$w] if year==`l', robust small
estadd local arci = e(ar_cset)	// Anderson-Rubin CI
di e(arci)
gen ci`l' = e(arci) 	// saves it as a varible in data
split ci`l', parse(,)	// split at ,
replace ci`l'1 = subinstr(ci`l'1, "[", "", .) // removes [
replace ci`l'2 = subinstr(ci`l'2, "]", "", .) // removes ]
destring ci`l'1 ci`l'2, replace
sum ci`l'1
return list
matrix C1[`i',2] = r(mean)	// Low-CI
sum ci`l'2
return list
matrix C1[`i',3] = r(mean)	// High-CI
drop ci`l' ci`l'1 ci`l'2

local ++i 
}

	// Restricted budget
matrix C2 = J(5,3,.)
local i=1
levelsof year if year!=2013, local(levels) 
foreach l of local levels {
qui ivreg2 totexp_excl ($x = $z) $yyctrl [aw=$w] if year==`l', robust small
matrix C2[`i',1] = _b[$x]

qui twostepweakiv 2sls totexp_excl ($x = $z) $yyctrl [aw=$w] if year==`l', robust small
estadd local arci = e(ar_cset)	// Anderson-Rubin CI
di e(arci)
gen ci`l' = e(arci) 	// saves it as a varible in data
split ci`l', parse(,)	// split at ,
replace ci`l'1 = subinstr(ci`l'1, "[", "", .) // removes [
replace ci`l'2 = subinstr(ci`l'2, "]", "", .) // removes ]
destring ci`l'1 ci`l'2, replace
sum ci`l'1
return list
matrix C2[`i',2] = r(mean)	// Low-CI
sum ci`l'2
return list
matrix C2[`i',3] = r(mean)	// High-CI
drop ci`l' ci`l'1 ci`l'2

local ++i 
}

	// Plot
matrix C1=C1'
	matrix C1[1,2] = 0	// Set to zero in 2015. Low first stage F-stat.
	matrix C1[2,2] = 0
	matrix C1[3,2] = 0
matrix C2=C2'
	matrix C2[1,2] = 0	// Set to zero in 2015. Low first stage F-stat.
	matrix C2[2,2] = 0
	matrix C2[3,2] = 0

coefplot (matrix(C1[1]), ci((C1[2] C1[3])) label(All budget) ///
yline(0, lc(gs4)) ciopts(recast(rcap))) ///
(matrix(C2[1]), ci((C2[2] C2[3])) m(D) msize(medsmall) label(Restricted budget) ///
ciopts(recast(rcap)) graphregion(color(white)) ///
xtitle("Estimated effect", size(small)) xlab(, labsize(small))), vertical ///
coeflabel(1 r1 = "2014" ///
		  2 r2 = "2015" ///
		  3 r3 = "2016" ///
		  4 r4 = "2017" ///
		  5 r5 = "2018" , labsize(small))
